<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2017/4/17
 * Time: 15:00
 */
use \think\Db;

function covertstr2arr($str,$pattern){
    $result = array();
    $params = explode ($pattern,$str);
    for($i = 0;$i < count($params);$i=$i+2){
        $result[$params[$i]] = $params[$i+1];
    }
    return $result;
}
function sp_portail_menu($pid){
    $menu = '';
    if($pid == 0){
        $menu .= "<ul class=\"titleUl\">";
    }else{
        $menu .= "<ul>";
    }
    $where['menu_pid'] = $pid;
    $where['type'] = 2;
    $where['status'] = 1;
    $menus = Db::table('ig_menu')->where($where)->order("listorder desc")->field("id,name,action,icon,data,rule_value")->select();
    if(!empty($menus)){
        foreach ($menus as $item){
            $menu .= "<li>";
            $param = empty($item['data']) ? [] : covertstr2arr($item['data'], "/");
            if(isset($param['p'])){
                $menu .= "<a href = '" . url($item['rule_value'], empty($item['data']) ? [] : covertstr2arr($item['data'], "/")) . "#".$param['p']."'>" . $item['name'] . "</a>";
            }else {
                $menu .= "<a href = '" . url($item['rule_value'], empty($item['data']) ? [] : covertstr2arr($item['data'], "/")) . "'>" . $item['name'] . "</a>";
            }
            $menu .= sp_portail_menu($item['id']);
            $menu .= '</li>';
        }
    }
    $menu .= "</ul>";
    return $menu;
}

function sp_left_menu($pid){
    $menu = '';

    $where['menu_pid'] = $pid;
    $where['type'] = 3;
    $where['status'] = 1;
    $menus = Db::table('ig_menu')->where($where)->order("listorder desc")->field("id,name,action,icon,data,rule_value")->select();
    if(!empty($menus)){
        if($pid == 0){
            $menu .= "<ul class = 'nav'>";
        }else{
            $menu .= "<ul class = 'sNav'>";
        }
        foreach($menus as $item){
            $submenu = sp_left_menu($item['id']);
            $menu .= "<li>";
            if(!empty($submenu)){
                $menu .= "<a  href = '" . (substr($item['rule_value'],strripos($item['rule_value'],"/") + 1)=="default"?"#":url($item['rule_value'], empty($item['data']) ? [] : covertstr2arr($item['data'], "/")) ). "'><i class='".$item['icon']."'></i>" . $item['name'] . "<img class=\"fr arrow\" src=\"/static/images/arrowUp.png\" alt=\"\"></a>";
                $menu .= $submenu;
            }else{
                $menu .=  "<a class = 'nav-link nav-dropdown-toggle' href = '" . (substr($item['rule_value'],strripos($item['rule_value'],"/") + 1)=="default"?"#":url($item['rule_value'], empty($item['data']) ? [] : covertstr2arr($item['data'], "/"))). "'><i class='".$item['icon']."'></i>" . $item['name'] . "</a>";
            }
            $menu .= "</li>";
        }
        $menu .= "</ul>";
    }

    return $menu;
}

/**
 * p12证书签名
 *
 */

function sign($params, $keyPath, $pwd){
    $params_str = arrToQuery($params, false);
    $rtValue = array();
    $pkcs12 = file_get_contents($keyPath);
    openssl_pkcs12_read($pkcs12,$certs,$pwd);
    $privateKey = $certs['pkey'];
    $publicKey = $certs['cert'];
    $merchantCert = strtoupper(bin2hex(base64_decode($publicKey)));
    $merchantCert = substr($merchantCert,24,strlen($merchantCert));
    $merchantCert = substr($merchantCert,0,strlen($merchantCert)-22);
    openssl_sign($params_str,$binarySignature,$privateKey,OPENSSL_ALGO_SHA1);
    $merchantSign = strtoupper(bin2hex($binarySignature));
    $rtValue['cert']=$merchantCert;
    $rtValue['sign']=$merchantSign;
    return $rtValue;
}
//RSA验证签名
function rsaVerify($signData,$merchantSign,$serverCert)
{
    $cert111 = base64_encode(hex2bin($serverCert));
    $cert111 = "-----BEGIN CERTIFICATE-----\n".preg_replace('/(.{64})/',"$1\n",$cert111)."\n-----END CERTIFICATE-----";
    return openssl_verify($signData, hex2bin($merchantSign), $cert111);
}
function arrToQuery($arrayQuery, $urlEncode = true){
//    ksort($arrayQuery);
    $tmp = array();
    foreach ($arrayQuery as $k => $param) {
        $tmp[] = $k . '=' . ($urlEncode ? urlencode($param) : $param);
    }
    $params = implode('&', $tmp);
    return $params;
}

function getPrivateKey($keyPath, $pwd)
{
    $pkcs12 = file_get_contents($keyPath);
    openssl_pkcs12_read($pkcs12, $certs, $pwd);
    return $certs ['pkey'];
}
function getPublicKey($keyPath,$pwd){
    $pkcs12 = file_get_contents($keyPath);
    openssl_pkcs12_read($pkcs12, $certs, $pwd);
    return $certs ['cert'];
}
/**
 * 验签
 */

function verify($params , $pubkeyPath)
{
    $signature_str = $params ['signature'];
    $signature = base64_decode($signature_str);
    unset ($params ['signature']);
    $public_key = file_get_contents($pubkeyPath);
    $params_str = arrToQuery($params, false);
    $params_sha1x16 = sha1($params_str, false);

    $isSuccess = openssl_verify($params_sha1x16, $signature, $public_key);
    return $isSuccess;
}
